<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class SharesCategoriesController extends AppController {

    var $name = 'SharesCategories';
    var $components = array(
        'Session', 'Alias', 'Upload'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array(
        'Catshare'
    );

    function afterFilter() {
        //update cache
        $this->Catshare->cacheListAll();
        $this->Catshare->cacheListAllToArray();
    }

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - Quản lý danh mục mách nhỏ');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'title LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Catshare.ordering' => 'DESC'
            ),
        );
        $data = $this->paginate('Catshare');

        $this->set('total', $this->params['paging']['Catshare']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->Catshare->alias}.{$this->Catshare->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );

        if ($this->Catshare->deleteAll($conditions, true)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d danh mục", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Catshare->id = $params['cid'][0];
        } else {
            $this->Catshare->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }

        if (empty($this->data)) {
            $fields = array(
                'title', 'id', 'published'
            );

            $this->data = $this->Catshare->read($fields);
        }
    }

    function add() {
       $this->set('title_for_layout', Configure::read('site_name') . ' - Quản lý danh mục mách nhỏ');
    }

    function save() {
        $data = $this->data['Catshare'];

        $data['alias'] = $this->Alias->alias($data['title']);
        
        if(empty($data['id'])){
            $data['ordering']   =   $this->Catshare->nextOrder();
        }
        if (isset($data['id'])) {
            $this->Catshare->set('user_modified', $this->Auth->user('id'));
            $this->Catshare->set('user_modified_ip', $_SERVER['REMOTE_ADDR']);
        } else {
            $this->Catshare->set('user_created', $this->Auth->user('id'));
            $this->Catshare->set('user_created_ip', $_SERVER['REMOTE_ADDR']);
        }

        if ($this->Catshare->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Catshare->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'shares_categories', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'shares_categories', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->Catshare->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Catshare->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'shares_categories', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->Catshare->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Catshare->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'shares_categories', 'action' => 'index'));
    }
    function saveorder() {
        $ordering = $this->params['form']['order'];

        foreach ($ordering AS $id => $order) {
            $this->Catshare->set('id', $id);
            $this->Catshare->saveField('ordering', $order);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('action' => 'index'));
    }
}

?>
